﻿using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using NAGFwk.BL;
using NAGFwk.UI;
using SilkeyERP.BL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SilkeyERP.UI.Entities.Suppliers
{
    public partial class ContentView : BaseContentDetail
    {
        private IBaseController<Model.Suppliers> _controller = new SuppliersController();
        public override string ModuleName { get { return "Suppliers"; } }

        public ContentView()
        {
            InitializeComponent();
        }

        public override void ShowModule(bool firstShow)
        {
            base.ShowModule(firstShow);
            MapItLoad(_controller);
            this.gvBaseContentDetail.OptionsEditForm.CustomEditFormLayout = new CustomEditContentView();
            this.OnUpdateGrid += ContentView_OnUpdateGrid;
            this.OnCancel += ContentView_OnCancel;   
        }

        private void ContentView_OnUpdateGrid(object m, RowObjectEventArgs rowObjectEventArgs)
        {
            MapItUpdate(rowObjectEventArgs, _controller);
        }

        void ContentView_OnCancel()
        {
            RefreshAll();
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            MapItDelete(_controller);
        }

        private void bbiRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            RefreshAll();
        }

        private void RefreshAll()
        {
            _controller = MapItRefresh(new SuppliersController());
        }

        private void gvBaseContentDetail_EditFormShowing(object sender, EditFormShowingEventArgs e)
        {
            var view = sender as GridView;
            if (view != null)
            {
                var customEditForm = view.OptionsEditForm.CustomEditFormLayout as CustomEditContentView;
                var myCurrent = (Model.Suppliers)bsBaseContentDetail.Current;
                if (customEditForm != null)
                    customEditForm.InitData(ref myCurrent);
            }
        }

        protected override void ValidateRows(ValidateRowEventArgs e)
        {
            var current = (Model.Suppliers)e.Row;
            if (string.IsNullOrEmpty(current.Name)) { e.ErrorText = Properties.Resources.ErrorNameRequired; e.Valid = false; }
            if (string.IsNullOrEmpty(current.ContactName)) { e.ErrorText = "Nombre del Contacto es Requerido."; e.Valid = false; }            
        }
    }
}
